import { defineStore } from 'pinia'
import { merge } from 'lodash-es'
import { Local } from '@/core/storage'

export const manageTokenKey = 'manage-token'

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */  
export const useUserInfo = defineStore('userInfo', {
  state: (): UserInfosState => ({
    userInfos: {
      token: Local.get(manageTokenKey) || '',
      userName: '',
      photo: '',
      time: 0,
      roles: [],
      authBtnList: [],
    },
  }),
  actions: {
    async setUserInfos() {
      const userInfos = <UserInfos>await this.getUserInfo().catch(() => {})
      merge(this.userInfos, userInfos)
    },
    setUserName(userName: string) {
      this.userInfos.userName = userName
    },
    setPhoto(photo: string) {
      this.userInfos.photo = photo
    },
    setToken(token: string) {
      this.userInfos.token = token
      Local.set(manageTokenKey, token)
    },
    getToken() {
      const token = Local.get(manageTokenKey)
      this.userInfos.token = token
      return token
    },
    removeToken() {
      this.userInfos.token = ''
      Local.remove(manageTokenKey)
    },
    clear() {
      this.userInfos.token = ''
      Local.remove(manageTokenKey)
      window.location.reload()
    },
    //查询用户信息
    async getUserInfo() {
      return new Promise((resolve, reject) => {
    
      })
    },
  },
})
